package com.example.pc.greenplants;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pc.greenplants.First.Main2Activity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Login extends AppCompatActivity {

    private Button glogin;
    private EditText guser;
    private EditText gpsw;
    private Button glog;
    private TextView gfor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        glogin = findViewById(R.id.btn_login);
        guser = findViewById(R.id.et_user);
        gpsw = findViewById(R.id.et_paw);

        glog = findViewById(R.id.btn_log);
        glog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Logon.class);
                startActivity(intent);
            }
        });
        add_login();
        Bmob.initialize(this, "fa96b598aa0534b09043ff71c1b27ace");
    }

    public void add_login() {
        glogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String luser = guser.getText().toString().trim();
                String lpsw = gpsw.getText().toString().trim();
                if (TextUtils.isEmpty(luser) || TextUtils.isEmpty(lpsw)) {
                    Toast.makeText(Login.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                final BmobUser buser = new BmobUser();
                buser.setUsername(luser);
                buser.setPassword(lpsw);
                buser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null) {


                            Intent intent = new Intent(Login.this, Main2Activity.class);
                            startActivity(intent);
                            Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(Login.this, "用户名或密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}
